package cn.factorybox.item.controller;

import cn.factorybox.common.entity.Constant;
import cn.factorybox.common.entity.Result;
import cn.factorybox.item.annotation.ControllerLogAnnotation;
import cn.factorybox.item.annotation.Log;
import cn.factorybox.item.enums.OperationType;
import cn.factorybox.item.enums.OperationUnit;
import cn.factorybox.item.service.UserService;
import cn.factorybox.model.entity.Company;
import cn.factorybox.model.entity.User;
import cn.factorybox.model.entity.UserUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author flypig
 * @version 1.0
 * @date 2020-03-28 11:11
 */

@Api(tags = "用户账号管理")
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @ControllerLogAnnotation(moduleName = "用户账号管理", functionName = "用户注册")
    @ApiOperation(value = "用户注册", notes = "用户注册")
    @PostMapping("register")
    public Result register(@Valid User user) {
        this.userService.register(user);
        return Result.ok("成功注册用户");
    }

    @ControllerLogAnnotation(moduleName = "用户账号管理", functionName = "查询用户")
    @ApiOperation(value = "查询用户", notes = "查询用户")
    @GetMapping("query")
    public ResponseEntity<User> selectUser(@RequestParam(value = "username", required = false)String uName, @RequestParam(value = "password", required = false)String uPass, @RequestParam("userIp") String userIp) {
        User user = this.userService.selectUser(uName,uPass,userIp);
//        if(user == null) {
//            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
//        }
        return ResponseEntity.ok(user);
    }

    @ControllerLogAnnotation(moduleName = "用户账号管理", functionName = "删除用户")
    @ApiOperation(value = "删除用户", notes = "删除用户")
    @DeleteMapping("userdelete")
    public Result deleteUser(@RequestParam("id") String id) {
        userService.deleteUser(id);
        return Result.ok("删除用户成功");
    }

    @ControllerLogAnnotation(moduleName = "用户账号管理", functionName = "更新用户")
    @ApiOperation(value = "更新用户", notes = "更新用户")
    @PutMapping("update")
    public Result updateUser(@RequestBody User user)  throws Exception {
        String result = this.userService.updateUser(user);
        if (!StringUtils.isBlank(result)) {
            Map<String,Object> data = new HashMap<>();
            data.put("token",result);
            return Result.ok(data,"修改成功");
        }
        return Result.error(Constant.SUCCEED_CODE,"旧密码验证失败");
    }

    @ControllerLogAnnotation(moduleName = "用户账号管理", functionName = "获取管理员列表")
    @ApiOperation(value = "获取管理员列表", notes = "获取管理员列表")
    @GetMapping("list")
    public Result selectList() {
        Map<String,Object> map = userService.selectList();
        return Result.ok(map,"成功获取管理员列表");
    }

    @ControllerLogAnnotation(moduleName = "用户账号管理", functionName = "创建主管用户")
    @ApiOperation(value = "创建主管用户", notes = "创建主管用户")
    @PostMapping("/master/register")
    public Result saveMaster(@RequestBody User user) {
        userService.saveMaster(user);
        return Result.ok("成功创建主管用户");
    }

    @ControllerLogAnnotation(moduleName = "用户账号管理", functionName = "获取主管列表")
    @ApiOperation(value = "获取主管列表", notes = "获取主管列表")
    @GetMapping("/master/list")
    public Result selectAllMaster() {
        List<User> masterList = userService.selectAllMaster();
        return new Result(masterList,"成功获取主管列表");
    }

    @ControllerLogAnnotation(moduleName = "用户账号管理", functionName = "删除主管，同时删除对应公司所有的信息")
    @ApiOperation(value = "删除主管，同时删除对应公司所有的信息", notes = "删除主管，同时删除对应公司所有的信息")
    @DeleteMapping("/master/delete")
    public Result deleteMaster(@RequestParam("id") String id) {
        userService.deleteMaster(id);
        return Result.ok("删除主管成功");
    }

    @ControllerLogAnnotation(moduleName = "用户账号管理", functionName = "批量创建子用户")
    @ApiOperation(value = "批量创建子用户", notes = "批量创建子用户")
    @PostMapping("/master/createsubuser")
    public Result createSubUser(HttpServletRequest request,@RequestParam("number") Integer number) throws Exception {
        userService.insertSubUser(request,number);
        return Result.ok("批量创建子用户成功");
    }

    @ControllerLogAnnotation(moduleName = "用户账号管理", functionName = "获取子用户列表")
    @ApiOperation(value = "获取子用户列表", notes = "获取子用户列表")
    @GetMapping("/master/subuserlist")
    public Result selectSubUserList(HttpServletRequest request) throws Exception {
        List<User> userList = userService.selectSubUserList(request);
        Map<String,Object> map = new HashMap<>();
        map.put("data",userList);
        return Result.ok(map,"成功获取子用户列表");
    }

    @ControllerLogAnnotation(moduleName = "用户账号管理", functionName = "获取所属主管信息")
    @ApiOperation(value = "获取所属主管信息", notes = "获取所属主管信息")
    @GetMapping("/subuser/masterinfo")
    public Result getMasterInfomation(HttpServletRequest request) throws Exception {
        User master = userService.getMasterBelong(request);
        return new Result(master,"获取所属主管信息成功");
    }

    @ControllerLogAnnotation(moduleName = "用户账号管理", functionName = "获取所属公司信息")
    @ApiOperation(value = "获取所属公司信息", notes = "获取所属公司信息")
    @GetMapping("/companybelong")
    public Result getCompanyMessage(HttpServletRequest request) throws Exception {
        Company company = userService.getCompanyBelong(request);
        return new Result(company,"获取所属公司信息成功");
    }

    @ControllerLogAnnotation(moduleName = "用户账号管理", functionName = "验证用户名是否合格")
    @ApiOperation(value = "验证用户名是否合格", notes = "验证用户名是否合格")
    @GetMapping("/checkname")
    public Result checkUserName(@RequestParam("username") String username) {
        return new Result(userService.checkUserName(username),"返回数据成功");
    }


//    /**
//     * 这里写个controller来拦截是否登录，写入日志
//     * @return
//     */
//    @Log(detail = "登录了后台",level = 1,operationUnit = OperationUnit.USER,operationType = OperationType.SELECT)
//    @GetMapping("check")
//    public String checkLogin() {
//        return "验证成功";
//    }

}
